home *** CD-ROM | disk | FTP | other *** search
- # Copyright 1999-2005 Gentoo Foundation
- # Distributed under the terms of the GNU General Public License v2
- # $Id: rc.eselect 246 2005-12-05 23:07:53Z kugelfang $
-
- DESCRIPTION="Manage /etc/init.d scripts in runlevels"
- MAINTAINER="Danny van Dyk <kugelfang@gentoo.org>"
- SVN_DATE='$Date: 2005-12-05 23:07:53 +0000 (Mon, 05 Dec 2005) $'
- VERSION=$(svn_date_to_version "${SVN_DATE}")
-
- # global setting
- RC_SOFTLEVEL=/var/lib/init.d/softlevel
-
- # list_runlevels PRIVATE
- # list runlevels for file $1
- list_runlevels() {
- [[ ${#@} -eq 1 ]] || return
- local runlevels
- for x in ${ROOT}/etc/runlevels/* ; do
- [[ -d ${x} ]] || continue
- [[ -L ${ROOT}/etc/runlevels/${x##*/}/${file} ]] \
- && runlevels=(${runlevels[@]} "${x##*/}")
- done
- echo -ne "${runlevels[@]}"
- }
-
- # is_script PRIVATE
- # check if file $1 is a valid init script
- is_script() {
- local file=${1}
- [[ -n ${file} ]] \
- || return 1
- ( [[ -L ${file} ]] \
- && [[ ! -e $(dirname ${file})/$(readlink ${file}) ]] ) \
- && return 1
- [[ -e ${file} ]] \
- && [[ ${file%%.sh} == ${file} ]] \
- && [[ ${file%%\~} == ${file} ]] \
- && [[ -n `grep "^#\!/sbin/runscript" ${file}` ]]
- }
-
- # find_scripts PRIVATE
- # browse directory $1 for init scripts and return a list
- find_scripts() {
- local ret
- for file in ${1}/* ; do
- is_script ${file} \
- && ret=(${ret[@]} "${file##*/}")
- done
- echo -ne ${ret[@]}
- }
-
- # run_runscript PRIVATE
- # run RC_RUNSCRIPT with script $2- and command $1
- run_runscript() {
- local command=${1}
- shift
- write_list_start "${1}"
- shift
- for script in ${@} ; do
- is_script ${ROOT}/etc/init.d/${script} \
- && /sbin/runscript ${ROOT}/etc/init.d/${script} ${command}
- done
- }
-
- ### add action
-
- describe_add() {
- echo "Add script to existing runlevel(s)"
- }
-
- describe_add_parameters() {
- echo "<script>"
- echo "<runlevels>"
- }
-
- describe_add_options() {
- echo "script : Init script (from 'list' action)"
- echo "runlevels : Runlevels to add to (defaults to 'default')"
- }
-
- do_add() {
- [[ ${#@} -gt 0 ]] \
- || die -q "Please specify the init script to be added!"
- local script=${1##*/}
- [[ -e ${ROOT}/etc/init.d/${script} ]] \
- || die -q "Please specify a valid init script!"
- shift 1
- local runlevels=${@:-default}
- write_list_start "Adding $(highlight ${script}) to following runlevels"
- for runlevel in ${runlevels} ; do
- if [[ ! -d ${ROOT}/etc/runlevels/${runlevel} ]] ; then
- write_kv_list_entry ${runlevel} "[invalid]"
- continue
- else
- if [[ -L ${ROOT}/etc/runlevels/${runlevel}/${script} ]] ; then
- write_kv_list_entry ${runlevel} "[skipped]"
- else
- ln -sf \
- ${ROOT}/etc/init.d/${script} \
- ${ROOTi}/etc/runlevels/${runlevel}/${script} \
- && write_kv_list_entry ${runlevel} "[done]" \
- || write_kv_list_entry ${runlevel} "[failed]"
- fi
- fi
- done
- }
-
- ### delete action
-
- describe_delete() {
- echo "Delete script from existing runlevel(s)"
- }
-
- describe_delete_parameters() {
- echo "<script>"
- echo "<runlevels>"
- }
-
- describe_delete_options() {
- echo "script : Init script (from 'list' action)"
- echo "runlevels : Runlevels to delete from (defaults to 'default')"
- }
-
- do_delete() {
- [[ ${#@} -gt 0 ]] \
- || die -q "Please specify the init script to be deleted!"
- local script=${1##*/}
- [[ -e ${ROOT}/etc/init.d/${script} ]] \
- || die -q "Please specify a valid init script!"
- shift 1
- local runlevels=${@:-default}
- write_list_start "Deleting $(highlight ${script}) from following runlevels"
- for runlevel in ${runlevels} ; do
- if [[ ! -d ${ROOT}/etc/runlevels/${runlevel} ]] ; then
- write_kv_list_entry ${runlevel} "[invalid]"
- continue
- else
- if [[ -L ${ROOT}/etc/runlevels/${runlevel}/${script} ]] ; then
- rm ${ROOT}/etc/runlevels/${runlevel}/${script} \
- && write_kv_list_entry ${runlevel} "[done]" \
- || write_kv_list_entry ${runlevel} "[failed]"
- else
- write_kv_list_entry ${runlevel} "[skipped]"
- fi
- fi
- done
- }
-
- ### list action
-
- describe_list() {
- echo "List all available init scripts"
- }
-
- describe_list_parameters() {
- echo "<runlevel>"
- }
-
- describe_list_options() {
- echo "runlevel : Runlevel to list (defaults to all)"
- }
-
- do_list() {
- local dir file item
- if [[ -n ${1} ]] && [[ -d ${ROOT}/etc/runlevels/${1} ]] ; then
- dir=${ROOT}/etc/runlevels/${1}
- write_list_start "Init scripts to be started by runlevel $(highlight ${1})"
- elif [[ -z ${1} ]] ; then
- dir=${ROOT}/etc/init.d
- write_list_start "Available init scripts"
- else
- die -q "${1} is no valid runlevel!"
- fi
-
- for file in $(find_scripts ${dir}) ; do
- [[ ${dir##*/} = init.d ]] \
- && write_kv_list_entry ${file} "$(list_runlevels ${file})" \
- || echo " ${file}"
- # && echo " ${file} $(space $((20 - ${#file}))) | $(list_runlevels ${file})" \
- # || echo " ${file}"
- done
- }
-
- ### show action
-
- describe_show() {
- echo "Show init script status for current runlevel"
- }
-
- do_show() {
- local runlevel=$(< ${RC_SOFTLEVEL}) script stopped
- write_list_start "Status of init scripts in runlevel $(highlight ${runlevel})"
- for script in $(find_scripts ${ROOT}/etc/runlevels/${runlevel}) ; do
- stopped=true
- for x in started starting stopping failed broken ; do
- if [[ -L ${ROOT}/var/lib/init.d/${x}/${script} ]] ; then
- write_kv_list_entry ${script} "[${x}]"
- stopped=false
- fi
- done
- [[ ${stopped} == true ]] && write_kv_list_entry ${script} "[stopped]"
- done
- }
-
- ### start action
-
- describe_start() {
- echo "Start given set of init scripts manually"
- }
-
- describe_start_parameters() {
- echo "<scripts>"
- }
-
- describe_start_options() {
- echo "scripts : Init scripts to start"
- }
-
- do_start() {
- [[ ${#@} -gt 0 ]] \
- || die -q "Please specify the init script to be started!"
- run_runscript start "Starting init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
- }
-
- ### stop action
-
- describe_stop() {
- echo "Stop given set of init scripts manually"
- }
-
- describe_stop_parameters() {
- echo "<scripts>"
- }
-
- describe_stop_options() {
- echo "scripts : Init scripts to stop"
- }
-
- do_stop() {
- [[ ${#@} -gt 0 ]] \
- || die -q "Please specify the init script to be stopped!"
- run_runscript stop "Stopping init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
- }
-
- ### pause action
-
- describe_pause() {
- echo "Pauses given set of init scripts manually"
- }
-
- describe_pause_parameters() {
- echo "<scripts>"
- }
-
- describe_pause_options() {
- echo "scripts : Init scripts to pause"
- }
-
- do_pause() {
- [[ ${#@} -gt 0 ]] \
- || die -q "Please specify the init script to be paused!"
- run_runscript pause "Pausing init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
- }
-
- ### restart action
-
- describe_restart() {
- echo "Restart given set of init scripts"
- }
-
- describe_restart_parameters() {
- echo "<scripts>"
- }
-
- describe_restart_options() {
- echo "scripts : Init scripts to restart"
- }
-
- do_restart() {
- [[ ${#@} -gt 0 ]] \
- || die -q "Please specify the init script to be restarted!"
- run_runscript restart "Restarting init script$([[ ${#@} -gt 1 ]] && echo -ne 's')" ${@}
- }
-
- # vim: set ft=eselect :
-